#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <unistd.h>

ucontext_t fun_ctx, test_ctx;

void test();

void fun() {
  printf("this is %s...\n", __FUNCTION__);
  sleep(1);
  setcontext(&test_ctx);
}

void test() {
  printf("this is %s...\n", __FUNCTION__);
  sleep(1);
  setcontext(&fun_ctx);
}

int main() {
  getcontext(&test_ctx);
  getcontext(&fun_ctx);

  test_ctx.uc_stack.ss_sp = malloc(4096);
  test_ctx.uc_stack.ss_size = 4096;
  test_ctx.uc_link = &fun_ctx;
  makecontext(&test_ctx, test, 0);

  fun_ctx.uc_stack.ss_sp = malloc(4096);
  fun_ctx.uc_stack.ss_size = 4096;
  fun_ctx.uc_link = &test_ctx;
  makecontext(&fun_ctx, fun, 0);

  test();
  return 0;
}